// import moment from 'moment'

export function isString (obj) {
  return Object.prototype.toString.call(obj) === '[object String]'
}

export function isObject (obj) {
  return Object.prototype.toString.call(obj) === '[object Object]'
}

export function isHtmlElement (node) {
  return node && node.nodeType === Node.ELEMENT_NODE
}

export const isFunction = functionToCheck => {
  const getType = {}
  return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'
}

export const isUndefined = val => val === undefined

export const isDefined = val => val !== undefined && val !== null

export const isNumeric = value => !Number.isNaN(parseFloat(value)) && Number.isFinite(value)

export const transferDate = date => {
  if (date && typeof date === 'string') {
    return date.replace(/-/g, '/')
  }
  return date
}

// export const isDate = (date, format) => {
//   if (!date) return false
//   date = transferDate(date)
//   if (Number.isNaN(+new Date(date))) return false
//   const tempDate = moment.isMoment(date) ? date : moment(date)
//   const parsed = moment(tempDate, format, !!format)
//   return parsed.isValid()
// }

/**
 * @param arr 查找数组
 * @param property 查找属性
 * @param value 查找值
 * @des:查找到值后返回其值
 */
export const findArrayReturnValue = (arr, property, value, findKey) => {
  if (!Array.isArray(arr)) {
    return value
  }
  const obj = arr.find(x => x[property] === value)
  return obj ? obj[findKey] : value
}

/**
 * [getFileType 下载文档对应的类型]
 * @param  {[type]} type [description]
 * @param  {[type]} id   [description]
 * @return {[type]}      [description]
 */
export function getFileType (id) {
  const arr = [
    { type: 'doc', application: 'application/msword' },
    {
      type: 'docx',
      application:
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    },
    { type: 'dot', application: 'application/msword' },
    {
      type: 'dotx',
      application:
        'application/vnd.openxmlformats-officedocument.wordprocessingml.template'
    },
    { type: 'xls', application: 'application/vnd.ms-excel' },
    {
      type: 'xlsx',
      application:
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    },
    { type: 'ppt', application: 'application/vnd.ms-powerpoint' },
    {
      type: 'pptx',
      application:
        'application/vnd.openxmlformats-officedocument.presentationml.presentation'
    },
    { type: 'pdf', application: 'application/pdf' },
    { type: 'txt', application: 'text/plain' },
    { type: 'gif', application: 'image/gif' },
    { type: 'image', application: 'image/jpeg' },
    { type: 'image', application: 'image/png' },
    { type: 'css', application: 'text/css' },
    { type: 'html', application: 'text/html' },
    { type: 'htm', application: 'text/html' },
    { type: 'xsl', application: 'text/xml' },
    { type: 'xml', application: 'text/xml' },
    { type: 'video', application: 'video/mpeg' },
    { type: 'video', application: 'video/mpeg' },
    { type: 'video', application: 'video/mp4' },
    { type: 'avi', application: 'video/x-msvideo' },
    { type: 'movie', application: 'video/x-sgi-movie' },
    { type: 'bin', application: 'application/octet-stream' },
    { type: 'exe', application: 'application/octet-stream' },
    { type: 'so', application: 'application/octet-stream' },
    { type: 'dll', application: 'application/octet-stream' },
    { type: 'ai', application: 'application/postscript' },
    { type: 'dir', application: 'application/x-director' },
    { type: 'js', application: 'application/x-javascript' },
    { type: 'swf', application: 'application/x-shockwave-flash' },
    { type: 'xhtml', application: 'application/xhtml+xml' },
    { type: 'xht', application: 'application/xhtml+xml' },
    { type: 'zip', application: 'application/zip' },
    { type: 'mid', application: 'audio/midi' },
    { type: 'midi', application: 'audio/midi' },
    { type: 'mp3', application: 'audio/mpeg' },
    { type: 'rm', application: 'audio/x-pn-realaudio' },
    { type: 'rpm', application: 'audio/x-pn-realaudio-plugin' },
    { type: 'wav', application: 'audio/x-wav' },
    { type: 'bmp', application: 'image/bmp' },
    { type: 'zip', application: 'application/x-zip-compressed' }
  ]
  return findArrayReturnValue(arr, 'application', id, 'type')
}
